package org.example.kafka_test;

import lombok.extern.slf4j.Slf4j;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.core.metrics.ApplicationStartup;
import org.springframework.scheduling.annotation.EnableScheduling;

import java.net.InetAddress;


@SpringBootApplication
@EnableScheduling
@EnableCaching
@ServletComponentScan
@Slf4j
public class KafkaTestApplication {

    public static void main(String[] args) {
        SpringApplication springApplication = new SpringApplication(KafkaTestApplication.class);
        springApplication.addListeners();
        // 添加监听器
        springApplication.addListeners();

        // 启动应用并获取ApplicationContext
        ConfigurableApplicationContext context = SpringApplication.run(KafkaTestApplication.class, args);

        // 从ApplicationContext中获取Environment对象
        Environment environment = context.getEnvironment();

        // 获取端口号
        String port = environment.getProperty("server.port");
        // 获取本机IP地址
        try {
            String ip = InetAddress.getLocalHost().getHostAddress();
            // 打印端口号
            System.out.println("本机地址为：Application started on IP=====================================> " + ip + ":" + port);
        } catch (Exception e) {
            System.out.println("Unable to get local IP address, defaulting to localhost.");
            System.out.println("Application started on Port: " + port);
        }
    }

}
